import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:homepad/logger/logger.dart';

class DateTimeModel extends ChangeNotifier {
  String time = "--:--";
  String date = "----年--月";
  String day = "--";
  String weekday = "-";

  static String _twoDigits(int n) {
    if (n >= 10) return "$n";
    return "0$n";
  }

  void refresh() {
    var now = DateTime.now();

    date = "${now.year}年${now.month}月";
    day = "${now.day}";
    final weeks = ["日","一","二","三","四","五","六"];
    weekday = weeks.elementAt(now.weekday % 7);
    var str = "${now.hour}:${_twoDigits(now.minute)}";
    if (time != str) {
      logger.info(str);
      time = str;
      notifyListeners();
    }
  }

  DateTimeModel() {
    refresh();
    Timer.periodic(const Duration(seconds: 1), (_) {
      refresh();
    });
  }
}